package com.ec.vs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ec.common.domain.Result;
import com.ec.common.utils.BeanUtils;
import com.ec.vs.entity.VsAppointment;
import com.ec.vs.mapper.VsMapper;
import com.ec.vs.service.VsExploreService;
import com.ec.vs.vo.VsAppointmentVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class VsExploreServiceImpl implements VsExploreService {

    private final VsMapper mapper;

    @Override
    public Result<Object> listExploreAppointments(String name, String mobile, String status, int page, int size) {
        LambdaQueryWrapper<VsAppointment> qw = Wrappers.lambdaQuery();
        qw.eq(VsAppointment::getType, 1)      // 仅参观
                .like(StringUtils.hasText(name), VsAppointment::getName, name)
                .like(StringUtils.hasText(mobile), VsAppointment::getMobile, mobile);

        if (StringUtils.hasText(status)) {
            List<Integer> list = Arrays.stream(status.split(","))
                    .map(Integer::valueOf)
                    .collect(Collectors.toList());
            qw.in(VsAppointment::getStatus, list);
        }
        qw.orderByDesc(VsAppointment::getCreateTime);

        Page<VsAppointment> p = new Page<>(page, size);
        Page<VsAppointment> res = mapper.selectPage(p, qw);

        Map<String, Object> data = new HashMap<>();
        data.put("records", res.getRecords());
        data.put("total", res.getTotal());
        data.put("page", (int) res.getCurrent());
        data.put("size", (int) res.getSize());
        return Result.ok(data);
    }

    @Override
    public int updateExploreAppointment(VsAppointmentVo vo) {
        VsAppointment entity = new VsAppointment();
        BeanUtils.copyProperties(vo, entity);
        entity.setType(1);           // 强制设为参观
        return mapper.updateById(entity);
    }

    @Override
    public int cancelExploreAppointment(Long id) {
        return mapper.updateStatusToCanceled(id);
    }

    @Override
    public int approveExploreAppointment(Long id) {
        return mapper.updateStatusToApproved(id);
    }

    @Override
    public int deleteExploreAppointment(Long id) {
        return mapper.deleteById(id);
    }

    @Override
    public int createExploreAppointment(VsAppointmentVo vo) {
        VsAppointment entity = new VsAppointment();
        BeanUtils.copyProperties(vo, entity);

        entity.setType(1);           // 强制为“参观”
        entity.setStatus(0);         // 默认待确定
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        // elderId / elderName 不设值，数据库允许 NULL
        return mapper.insert(entity);
    }
}